package com.snda.lib.task;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;

public class BaseAsyncTask extends AsyncTask<Object, Object, Object> {

	protected Context context;
	protected WeakReference<ICallBack> objCallBack = null;

	protected long nId; // 当前Task的唯一表示标识符，用来区分同一样的ID，具体的实现由各个任务本身实现
	protected int nCookie = 1000; // 当前Task的ID
	protected Bundle mParams = null; // 传递的参数
	protected int nTaskType = HttpUtil.TASK_TYPE_INVALID; // Task的类型

	public BaseAsyncTask(Context context, ICallBack callback) {
		super();
		this.context = context;
		this.objCallBack = new WeakReference<ICallBack>(callback);
	}

	public ICallBack getCallBack() {
		if (objCallBack == null || objCallBack.get() == null)
			return null;

		return objCallBack.get();
	}

	public void cancelTask() {
		cancel(true);
		TaskManager.getInstance().RemoveTask(nCookie);
	}

	public long getId() {
		Random r = new Random();
		this.nId = Math.abs(r.nextLong());

		return this.nId;
	}

	public void setTaskType(int nTaskType) {
		this.nTaskType = nTaskType;
	}

	protected boolean checkParams(Object[] params) {

		if (params.length != 2) {
			return false;
		}

		if (params[0] != null && !Bundle.class.isInstance(params[0])) {
			return false;
		}

		return true;
	}

	@Override
	protected Map<String, Object> doInBackground(Object... params) {

		if (checkParams(params) == false) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			setErr(HttpUtil.ERROR_SUCCESS, mapData);
			mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_INVALID_PARAM);
			return mapData;
		}

		if (params[1] != null && Integer.class.isInstance(params[1])) {
			nCookie = (Integer) params[1];
		}

		// 完成实际的任务
		Map<String, Object> mapData = doTask(params);

		// 对返回的结果进行处理
		return process(mapData);
	}

	// 实际的完成后台任务的地方，通常由子类完成该任务
	protected Map<String, Object> doTask(Object[] params) {
		return null;
	}

	// 自行解释错误码
	protected String getCustomErrMsg(int nError) {
		return null;
	}

	protected Map<String, Object> checkResponse(Map<String, Object> mapData) {
		if (mapData == null)
			return null;

		// 追加返回码和错误描述信息
		mapData.put(HttpUtil.KEY_TASK_TYPE, nTaskType);

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null) {
			nError = HttpUtil.ERROR_UNKNOWN;
		}

		if (nError != HttpUtil.ERROR_SUCCESS) {
			setErr(nError, mapData);
			return mapData;
		}

		setErr(HttpUtil.ERROR_SUCCESS, mapData);
		return mapData;
	}

	protected Map<String, Object> process(Map<String, Object> mapData) {
		return checkResponse(mapData);
	}

	protected void setErr(int nError, Map<String, Object> mapData) {
		if (mapData == null)
			return;

		mapData.put(HttpUtil.KEY_ERROR_CODE, nError);

		String errmsg = HttpUtil.getErrorDesc(nError);
		if (errmsg == null || errmsg.equals("")) {
			errmsg = getCustomErrMsg(nError);
		}
		mapData.put(HttpUtil.KEY_ERROR_MSG, errmsg);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onPostExecute(Object result) {

		if (getCallBack() != null && result != null) {
			getCallBack().doCallBack((Map<String, Object>) result);
		}

		TaskManager.getInstance().RemoveTask(nCookie);
	}

}